# /usr/bin/python
# ! -*- coding:UTF-8 -*-

# 定义需要用的变量
# 每行上的按钮的个数
NUMBER_OF_ITEM = 20

# 定义并初始化一个保存所有按钮的数组
btns = []
for n_f_i_i in range(NUMBER_OF_ITEM):
    temp_x = []
    btns.append(temp_x)
    for n_f_i_j in range(NUMBER_OF_ITEM):
        btns[n_f_i_i].append(0)

        # 定义一个保存已经选择的按钮的数组
selected_btns = []


# 定义按钮的事件
def onclick(self, btn):
    # print "clicked button", self, ",btn ", btn
    if self["background"] == "white":
        self["background"] = "black"
        btn.set_selected(True)
        selected_btns.append(btn)
    else:
        self["background"] = "white"
        selected_btns.remove(btn)
        btn.set_selected(False)


def next_step():
    execute_game()


def execute_game():
    global selected_btns
    # print_all_selected_button()

    # 将每个selected的按钮周围的也加入到该selected_btns里
    all_around_btns = []  # 保存selected_btns中的所有按钮的周围的所有的按钮
    for ibtn in selected_btns:
        around_btns = list_around_cells(ibtn)
        all_around_btns += around_btns
    # 将all_around_btns添加到selected_btns中
    selected_btns += all_around_btns
    selected_btns = list(set(selected_btns))  # 去重
    print "after added len", len(selected_btns)
    print_all_selected_button()

    # 遍历已经选择了的按钮，判断每个已经选择了的按钮的周边的状态
    for btn in selected_btns:

        # 判断界限，将周边按钮加入selected_btns
        around_btns = list_around_cells(btn)

        # 计算周边活着的cell的数量(使用颜色判定，颜色是黑的才说明当前为活着)
        live_cell_number = 0
        for around in around_btns:
            if around.get_button()["background"] == "black":
                live_cell_number += 1

        # 处理周边按钮，加入计算逻辑
        if live_cell_number == 3:
            current = btn.get_button()
            btn.set_selected(True)
        elif live_cell_number == 2:
            pass
        else:
            current = btn.get_button()
            btn.set_selected(False)

    # 所有的按钮遍历完成，根据selected属性更改颜色。
    for item in selected_btns:
        if item.get_selected():
            item.get_button()["background"] = "black"
        else:
            item.get_button()["background"] = "white"

    # 随后开始从已选择的cell（按钮）中去除死掉的cell
    selected_btns = filter(lambda x: x.get_selected(), selected_btns)  # 打印保存所有按钮的数组


# 将该按钮周边按钮也加入selected_btn队列
def list_around_cells(ibtn):
    i = ibtn.get_x()
    j = ibtn.get_y()

    around_btns = []  # 保存周边的按钮

    max_size = NUMBER_OF_ITEM - 2

    # 左上
    if i >= 1 and j >= 1:
        around_btns.append(btns[i - 1][j - 1])
    # 正上
    if i >= 1:
        around_btns.append(btns[i - 1][j])
    # 右上
    if i >= 1 and j <= max_size:
        around_btns.append(btns[i - 1][j + 1])
    # 正左
    if j >= 1:
        around_btns.append(btns[i][j - 1])
    # 正右
    if j <= max_size:
        around_btns.append(btns[i][j + 1])
    # 左下
    if i <= max_size and j >= 1:
        around_btns.append(btns[i + 1][j - 1])
    # 正下
    if i <= max_size:
        around_btns.append(btns[i + 1][j])
    # 右下
    if i <= max_size and j <= max_size:
        around_btns.append(btns[i + 1][j + 1])

    # 返回该按钮附近的按钮队列
    return around_btns


def print_all_button():
    print "*****number of buttons:", NUMBER_OF_ITEM * NUMBER_OF_ITEM, "*****"
    for ii in range(NUMBER_OF_ITEM):
        for jj in range(NUMBER_OF_ITEM):
            print btns[ii][jj]
    print "*****print finish******"


# 打印已经选择的所有的按钮
def print_all_selected_button():
    print "*****selected buttons:", len(selected_btns), "*****"
    for ii in range(len(selected_btns)):
        print selected_btns[ii]
    print "*****print finish******"
